<template>
  <div class="activity-setting">
      <Breadcrumb>
          <BreadcrumbItem>活动设置</BreadcrumbItem>
          <BreadcrumbItem>活动配置</BreadcrumbItem>
      </Breadcrumb>
      <Form ref="data" :model="data" :rules="rules" class="form">
          <FormItem label="会员可开的宝箱数量(0=不限制)" prop="init_bz_number">
              <Input type="text" v-model.number="data.init_bz_number" placeholder="会员可开的宝箱数量(0=不限制)"></Input>
          </FormItem>
          <FormItem label="打开宝箱的距离(建议5-10米,0=不限制开箱距离)" prop="open_distance">
              <Input type="text" v-model.number="data.open_distance" placeholder="打开宝箱的距离(建议5-10米)"></Input>
          </FormItem>
          <FormItem label="地图宝箱显示数量(0=系统自动计算显示数量，建议数量不超过1000)" prop="bz_number">
              <Input type="text" v-model.number="data.bz_number" placeholder="地图宝箱显示数量(0=系统自动计算显示数量，建议数量不超过1000)"></Input>
          </FormItem>
          <FormItem v-if="shop_model == 1" label="是否强制用户前往活动区域后才可以领取门店卡券">
              <br /><i-switch v-model="get_coupon_bourn"></i-switch>
          </FormItem>
          <FormItem :label="'宝藏中奖率(0-10,0=最后发放) 当前中奖率'+data.bz_win_odds*10+'%'" prop="bz_win_odds">
              <Input type="text" v-model.number="data.bz_win_odds" placeholder="宝藏中奖率(0-10)"></Input>
          </FormItem>
          <FormItem :label="'实物中奖率(0-10,0=最后发放) 当前中奖率'+data.entity_win_odds*10+'%'" prop="entity_win_odds">
              <Input type="text" v-model.number="data.entity_win_odds" placeholder="实物中奖率(0-10)"></Input>
          </FormItem>
          <FormItem v-if="shop_model == 1 || test_coupon" :label="'卡券中奖率(0-10,0=最后发放) 当前中奖率'+data.coupon_win_odds*10+'%'" prop="coupon_win_odds">
              <Input type="text" v-model.number="data.coupon_win_odds" placeholder="卡券中奖率(0-10)"></Input>
          </FormItem>
          <FormItem>
              <Button @click="submit('data')">确认设置</Button>
          </FormItem>
      </Form>
  </div>
</template>

<script>
export default {
  name: 'activity-setting',
  data(){
    return {
        activity_id:sysinfo.activity_id,
        shop_model:0,  //活动是否开启了商场模式
        test_coupon:false,  //活动后台是否投放了卡券
        data:{
            init_bz_number:0,
            open_distance:5,
            bz_number:0,
            bz_win_odds:0,
            entity_win_odds:0,
            coupon_win_odds:0,
            get_coupon_bourn:1,  //是否强制用户到活动区域后才可以领券
        },
        get_coupon_bourn:true,//是否强制用户到活动区域后才可以领券,布尔值
        rules:{
            init_bz_number:[
                {type:'number',required:true,message:'请输入正确数量'},
                {pattern:/^\d+$/,message:'请输入正确数量'}
            ],
            open_distance:[
                {type:'number',required:true,message:'请输入正确数量'},
                {pattern:/^\d+$/,message:'请输入正确的距离'}
            ],
            bz_number:[
                {type:'number',required:true,message:'请输入正确数量'},
                {pattern:/^\d+$/,message:'请输入正确数量'}
            ],
            bz_win_odds:[
                {type:'number',max:10,min:0,required:true,message:'请输入正确的中奖率'},
                {pattern:/^\d+$/,message:'请输入正确的中奖率'}
            ],
            entity_win_odds:[
                {type:'number',max:10,min:0,required:true,message:'请输入正确的中奖率'},
                {pattern:/^\d+$/,message:'请输入正确的中奖率'}
            ],
            coupon_win_odds:[
                {type:'number',max:10,min:0,required:true,message:'请输入正确的中奖率'},
                {pattern:/^\d+$/,message:'请输入正确的中奖率'}
            ]
        }
    }
  },
  created(){
    this.find();
    this.testPutCoupon();
  },
  watch:{
      "data.bz_win_odds"(){
          this.oddsPar('bz_win_odds');
      },
      "data.entity_win_odds"(){
          this.oddsPar('entity_win_odds');
      },
      "data.coupon_win_odds"(){
          this.oddsPar('coupon_win_odds');
      },
      "get_coupon_bourn"(){
          if(this.get_coupon_bourn){
                this.data.get_coupon_bourn = 1;
          }else{
                this.data.get_coupon_bourn = 0;
          }
      }
  },
  methods:{
    //获取活动信息
    find(){
        Axios.post(sysinfo.url,Qs.stringify({
            pages:'activity',
            doing:'easyFind',
            activity_id:this.activity_id,
            return:['init_bz_number','open_distance','bz_number','bz_win_odds','entity_win_odds','coupon_win_odds','shop_model','get_coupon_bourn']
        }))
            .then((res)=>{
                if(res.data.status == 'success'){
                    var {init_bz_number,open_distance,bz_number,bz_win_odds,entity_win_odds,coupon_win_odds,shop_model,get_coupon_bourn} = res.data.data;
                    this.shop_model = parseInt(shop_model);
                    this.data ={
                        init_bz_number:parseInt(init_bz_number),
                        open_distance:parseInt(open_distance),
                        bz_number:parseInt(bz_number),
                        bz_win_odds:parseInt(bz_win_odds),
                        entity_win_odds:parseInt(entity_win_odds),
                        coupon_win_odds:parseInt(coupon_win_odds),
                        get_coupon_bourn:parseInt(get_coupon_bourn)
                    }
                    this.get_coupon_bourn = this.data.get_coupon_bourn ? true : false;
                }
            })
            .catch(error=>console.log(error));
    },
    //检测活动是否投放了卡券
    testPutCoupon(){
      Axios.post(sysinfo.url,Qs.stringify({
          pages:'sponsorCoupon',
          doing:'testPutCoupon',
          activity_id:this.activity_id
      }))
          .then((res)=>{
              if(res.data.status == 'success'){
                  this.test_coupon = true;
              }
          })
          .catch(error=>console.log(error));
    },
    //联动各种类型的奖品平均中奖率
    oddsPar(odds){
        if(this.data.bz_win_odds === '' || this.data.entity_win_odds === '' || this.data.coupon_win_odds === '') return;
        var total = parseInt(this.data.bz_win_odds) + parseInt(this.data.entity_win_odds);
        if(this.shop_model == 1 || this.test_coupon) total += parseInt(this.data.coupon_win_odds);
        if(total != 10){
            if(odds != 'bz_win_odds'){
                if(total > 10 && this.data.bz_win_odds > 0){
                    this.data.bz_win_odds -= 1;
                    this.oddsPar(odds);
                    return;
                }
                if(total < 10 && this.data.bz_win_odds < 10){
                    this.data.bz_win_odds += 1;
                    this.oddsPar(odds);
                    return;
                }
            }
            if(odds != 'entity_win_odds'){
                if(total > 10 && this.data.entity_win_odds > 0){
                    this.data.entity_win_odds -= 1;
                    this.oddsPar(odds);
                    return;
                }
                if(total < 10 && this.data.entity_win_odds < 10){
                    this.data.entity_win_odds += 1;
                    this.oddsPar(odds);
                    return;
                }
            }
            if(odds != 'coupon_win_odds' && this.shop_model == 1){
                if(total > 10 && this.data.coupon_win_odds > 0){
                    this.data.coupon_win_odds -= 1;
                    this.oddsPar(odds);
                    return;
                }
                if(total < 10 && this.data.coupon_win_odds < 10){
                    this.data.coupon_win_odds += 1;
                    this.oddsPar(odds);
                    return;
                }
            }
        }
    },
    submit(data){
        this.$refs[data].validate((valid) => {
            if(valid){
                Axios.post(sysinfo.url,Qs.stringify({
                    pages:'activity',
                    doing:'edit',
                    activity_id:this.activity_id,
                    data:this.data
                }))
                    .then((res)=>{
                        if(res.data.status == 'success'){
                            this.$Message.success('设置成功');
                        }else{
                            this.$Message.error('设置失败');
                        }
                    })
                    .catch(error=>console.log(error));
            }
        })
    }
  }
}
</script>

<style scoped>
    .activity-setting{
        padding:0 10px 20px 10px;
    }
    .form{
        width:400px;
    }
</style>
